package com.newway.wechat;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newway.wechat.entity.sign.SignDataEntity;
import com.newway.wechat.enums.SignStatusEnum;
import com.newway.wechat.mapper.SignDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
@EnableScheduling
public class SignScheduled {

    @Resource
    SignDataMapper signDataMapper;

    @Scheduled(fixedRate = 60 * 60 * 1000) //一个小时执行一次
    public void executeTask() {
        QueryWrapper<SignDataEntity> signDataEntityQueryWrapper = new QueryWrapper<>();
        signDataEntityQueryWrapper.eq("sign_status", SignStatusEnum.PUBLISHED.code);
        List<SignDataEntity> signDataEntities = signDataMapper.selectList(signDataEntityQueryWrapper);
        for(int i=0;i<signDataEntities.size();i++){
            SignDataEntity signDataEntity = signDataEntities.get(i);
            if(signDataEntity.getEndTime().isBefore(LocalDateTime.now())){
                signDataEntity.setSignStatus(SignStatusEnum.END.code);
                signDataMapper.updateById(signDataEntity);
            }
        }
    }
}
